package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.SpecialtyFragmentBean;

import java.util.List;

/**
 * 特产类界面左边布局适配器
 */

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.LeftViewHolder> {

    private Context mContext;
    private RecyclerView mRcv;
    private List<SpecialtyFragmentBean.ResultBean.ListBean> mData;
    private int checkedItem = -1;                //选中的item项
    private OnItemClickListener mItemClickListener;

    public CategoryAdapter(Context mContext, RecyclerView recyclerView, List<SpecialtyFragmentBean.ResultBean.ListBean> mData) {
        this.mContext = mContext;
        this.mRcv = recyclerView;
        this.mData = mData;
    }

    public void updateCheck(int position) {
        resetCheck();
        SpecialtyFragmentBean.ResultBean.ListBean bean = mData.get(position);
        bean.setChecked(true);
        mData.set(position, bean);
        checkedItem = position;
        mRcv.smoothScrollToPosition(position);
        notifyDataSetChanged();
    }

    public List<SpecialtyFragmentBean.ResultBean.ListBean> getData() {
        return mData;
    }

    public int getCheckedItem() {
        return checkedItem;
    }

    public void setCheckedItem(int checkedItem) {
        this.checkedItem = checkedItem;
    }

    private void resetCheck() {
        for (int i = 0; i < mData.size(); i++) {
            SpecialtyFragmentBean.ResultBean.ListBean bean = mData.get(i);
            bean.setChecked(false);
            mData.set(i, bean);
        }
    }

    @Override
    public CategoryAdapter.LeftViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LeftViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_sort_list, parent, false));
    }

    @Override
    public void onBindViewHolder(LeftViewHolder holder, final int position) {
        if (mData.get(position).isChecked()) {
            holder.tvCategory.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent));
            holder.tvCategory.setBackground(ContextCompat.getDrawable(mContext, R.color.item_bg));
        } else {
            holder.tvCategory.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            holder.tvCategory.setBackground(ContextCompat.getDrawable(mContext, R.color.divider));
        }
        holder.tvCategory.setText(mData.get(position).getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateCheck(position);
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(view, position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class LeftViewHolder extends RecyclerView.ViewHolder {

        TextView tvCategory;

        public LeftViewHolder(View itemView) {
            super(itemView);
            tvCategory = itemView.findViewById(R.id.tv_sort);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
